package com.estronger.shopmanage.fragment.install.pg;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.estronger.pull_to_refresh_expandablelist.CommonFooterView.State;
import com.estronger.pull_to_refresh_expandablelist.PullToRefreshBase;
import com.estronger.pull_to_refresh_expandablelist.PullToRefreshBase.OnRefreshListener;
import com.estronger.pull_to_refresh_expandablelist.PullToRefreshListView;
import com.estronger.shopmanage.R;
import com.estronger.shopmanage.adapter.tasking.InstallDPGListAdapter;
import com.estronger.shopmanage.adapter.tasking.InstallYPGListAdapter;
import com.estronger.shopmanage.base.MyApplication;
import com.estronger.shopmanage.bean.InstallDPG;
import com.estronger.shopmanage.bean.InstallYPG;
import com.estronger.shopmanage.manager.NetManager;
import com.estronger.shopmanage.ui.install.pg.InstallPGActivity;
import com.estronger.shopmanage.utils.NetUtils;
import com.estronger.shopmanage.utils.Toast;
import com.estronger.shopmanage.view.CircularProgress;
import com.google.gson.Gson;
import com.lidroid.xutils.http.RequestParams;

import cn.jesse.nativelogger.NLogger;

/**
 * 送货的五个Fregment的公共父类
 * 
 * @author Administrator
 */
public abstract class InstallPGBaseFragment extends Fragment implements
		OnRefreshListener<ListView> {//
	protected PullToRefreshListView mListView;
	protected RequestParams params;
	protected RelativeLayout root;
	protected CircularProgress bar;
	protected TextView empty_view;
	protected InstallDPGListAdapter adapterdp;
	protected InstallYPGListAdapter adapteryp;
	private InstallDPG data;
	private InstallDPG tmpdata;
	private InstallYPG data2;
	private InstallYPG tmpdata2;
	Gson gson = new Gson();
	private InstallPGActivity activity;
	public String type = "";
	private int pageindex = 1;
	private String pagesize = "10";
	boolean isFrist = false;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
							 Bundle savedInstanceState) {
		activity = (InstallPGActivity) getActivity();
		root = (RelativeLayout) View.inflate(activity,
				R.layout.fragment_history, null);
		mListView = (PullToRefreshListView) root
				.findViewById(R.id.success_listview);

		bar = (CircularProgress) root.findViewById(R.id.loding_view);
		empty_view = (TextView) root.findViewById(R.id.empty_view);
		mListView.setOnRefreshListener(this);
		//mListView.setMode(Mode.BOTH);
		mListView.setState(State.HIDE);

		return root;
	}

	protected abstract boolean needConnect();

	protected abstract String gettype();

	public void connect(String gettype,boolean isFrist) {
		this.isFrist = isFrist;
		if(isFrist){
			data = null;
			data2 = null;
            pageindex = 1;
		}
		params = new RequestParams();
		params.addBodyParameter("usercode", MyApplication.getUserCode());
		params.addBodyParameter("gettype", gettype);
		params.addBodyParameter("pagesize", pagesize);
		params.addBodyParameter("pageindex", String.valueOf(pageindex));
		params.addBodyParameter("sf_action", "getistassignlist");
		netManager.connectWork(params, NetManager.INSTALL_PG);
	}

	protected NetManager netManager = new NetManager() {
		@Override
		public void onNetWorkErorr(int code, String result) {
			try {
				if (null != mListView) {
					mListView.setState(State.HIDE);
					if (null != bar)
						bar.setVisibility(View.GONE);
					mListView.onRefreshComplete();
					if (data.data.list.size() == 0) {
						empty_view.setVisibility(View.VISIBLE);
					}
				}
			}catch (Exception e){
				NLogger.e(""+e.getMessage());
			}
		}

		@Override
		public void onNetWorkSuccess(int code, String result) {
			if (null != bar)
				bar.setVisibility(View.GONE);
			if (null != mListView) {
				mListView.setState(State.HIDE);
				mListView.onRefreshComplete();
				if (code == NetManager.INSTALL_PG) {
					reflashdata(gettype(),result);
				}
			}
		}
	};

	private void reflashdata(String gettype,String result){
		type = gettype;
		if (null != bar) bar.setVisibility(View.GONE);
		if (null != mListView) {
			mListView.setState(State.HIDE);
			mListView.onRefreshComplete();
		}
		if(type.equals("0")){
			tmpdata = gson.fromJson(result, InstallDPG.class);
			if(data == null || data.data.list.size() == 0){
				adapterdp = new InstallDPGListAdapter(activity, tmpdata.data.list);
				data = tmpdata;

                if(tmpdata.data.list.size() >= Integer.parseInt(pagesize)){
                    mListView.setMode(PullToRefreshBase.Mode.BOTH);
                }
			}else if(data.data.list.size()>0){
				if(tmpdata.data.pageindex == data.data.pageindex){
					return;
				}
				for(int i=0;i<tmpdata.data.list.size();i++){
					data.data.list.add(tmpdata.data.list.get(i));
				}
				if(tmpdata.data.list.size() == 0){
					Toast.show(activity,"没有更多了！",2000);
				}
				adapterdp = new InstallDPGListAdapter(activity, data.data.list);
                mListView.setSelection((pageindex-1)*Integer.parseInt(pagesize));
                //延时一会，然后自动定位到最后一条记录(必须要重新定位，不要删除)
				new Handler().postDelayed(new Runnable() {
					@Override
					public void run() {
						mListView.setSelection((pageindex-1)*Integer.parseInt(pagesize));
					}
				},500);
			}

			mListView.setAdapter(adapterdp);
			adapterdp.notifyDataSetChanged();
			if (data.data.list.size() > 0
					&& empty_view.getVisibility() == View.VISIBLE) {
				empty_view.setVisibility(View.GONE);
			}
			if (data.data.list.size() == 0) {
				empty_view.setVisibility(View.VISIBLE);
			}
		}else{
			tmpdata2 = gson.fromJson(result, InstallYPG.class);
			if(data2 == null || data2.data.list.size() == 0){
				adapteryp = new InstallYPGListAdapter(activity, tmpdata2.data.list);
				data2 = tmpdata2;

                if(tmpdata2.data.list.size() >= Integer.parseInt(pagesize)){
                    mListView.setMode(PullToRefreshBase.Mode.BOTH);
                }
			}else if(data2.data.list.size()>0){
				if(tmpdata2.data.pageindex == data2.data.pageindex){
					return;
				}
				for(int i=0;i<tmpdata2.data.list.size();i++){
					data2.data.list.add(tmpdata2.data.list.get(i));
				}
				if(tmpdata2.data.list.size() == 0){
					Toast.show(activity,"没有更多了！",2000);
				}
				adapteryp = new InstallYPGListAdapter(activity, data2.data.list);
                mListView.setSelection((pageindex-1)*Integer.parseInt(pagesize));
                //延时一会，然后自动定位到最后一条记录(必须要重新定位，不要删除)
				new Handler().postDelayed(new Runnable() {
					@Override
					public void run() {
						mListView.setSelection((pageindex-1)*Integer.parseInt(pagesize));
					}
				},500);
			}


			mListView.setAdapter(adapteryp);
			adapteryp.notifyDataSetChanged();
			if (data2.data.list.size() > 0
					&& empty_view.getVisibility() == View.VISIBLE) {
				empty_view.setVisibility(View.GONE);
			}
			if (data2.data.list.size() == 0) {
				empty_view.setVisibility(View.VISIBLE);
			}
		}
	}



	protected void loadData() {
		if (!NetUtils.isNetworkConnected(getActivity())) {
			mListView.onRefreshComplete();
			Toast.show(activity, "网络异常", 2000);
		} else {
			if(activity==null){
				activity = (InstallPGActivity) getActivity();
			}else{
				activity.getTitleNum();
			}
			MyApplication.getHandler().postDelayed(new Runnable() {
				@Override
				public void run() {
					connect(gettype(),false);
				}
			}, 1000);
		}
	}

	@Override
	public void onRefresh(PullToRefreshBase<ListView> refreshView) {
		if (mListView.isHeaderShown()) {
			pageindex = 1;
			data = null;
			data2 = null;
			loadData();
		} else if(mListView.isFooterShown()) {
			pageindex+=1;
			loadData();
		}
		mListView.postDelayed(new Runnable() {
			@Override
			public void run() {
				mListView.onRefreshComplete();
			}
		}, 1000);
	}

}
